home *** CD-ROM | disk | FTP | other *** search
- -- sched.e
- -- Task Scheduler
-
- global constant HUGE_TIME = 1e30
-
- constant sample_interval = 1.0
- atom sample_count
-
- type reasonable_delay(atom x)
- return x > 0 and x < 30
- end type
-
- global procedure init_delay()
- -- since time() may not have fine enough
- -- resolution for small delays, we see how many for-loop iterations
- -- we can complete over a small sample period
-
- atom t
-
- t = time() + sample_interval
- for i = 1 to 999999999 do
- if time() < t then
- else
- sample_count = i
- exit
- end if
- end for
- end procedure
-
- global procedure delay(reasonable_delay t)
- -- delay for t seconds
- atom stop
- if t > sample_interval then
- -- time() should be precise enough
- stop = time() + t
- while time() < stop do
- end while
- else
- -- loop a certain number of times
- stop = time() + sample_interval
- for i = 1 to floor(t / sample_interval * sample_count) do
- if time() < stop then
- else
- end if
- end for
- end if
- end procedure
-
-
- global procedure sched(task t, positive_atom wait)
- -- schedule task to be reactivated in wait seconds
-
- if wait = 0 then
- -- deactivate
- tcb[t] = HUGE_TIME
- else
- -- activate in wait seconds from now
- tcb[t] = time() + wait
- end if
- end procedure
-
-
- global function next_task()
- -- choose the next task to be executed
-
- positive_atom mintime
- task mintask
-
- -- find task with minimum time
- mintime = HUGE_TIME
- for i = 1 to NTASKS do
- if tcb[i] < mintime then
- mintask = i
- mintime = tcb[i]
- end if
- end for
-
- -- subtract it's early-activation tolerance
- tcb[mintask] = tcb[mintask] - eat[mintask]
-
- -- wait until it is time to activate it
- while time() < tcb[mintask] do
- end while
-
- return mintask
- end function
-
-